export default {
    name: 'baesTable',
    props: {
        // 表格数据
        data: {
            type: Array,
            required: true,
            default: () => []
        },
        // 初始化时可以修改的字段
        fieldsCanModified: {
            type: Array,
            default: () => []
        },
        // 表格配置
        config: {
            type: Array,
            required: true,
            default: () => []
        },
        // 是否显示综合行
        showSummary: Boolean,
        // 是否可以增加
        canAdd: Boolean,
        // 是否固定表格高度
        height: Number,
        // 绑定表单model
        formSearch: Object,
        // 需要求和的列
        summaryColumn: Array
    },
    mounted() {
        this.$emit('searchHttp', {
            current: this.pagination.current,
            pageSize: this.pagination.pageSize,
            ...this.formSearch
        });
    },
    data() {
        return {
            pagination: {
                totalSize: 0,
                pageSize: 10,
                pageNum: 0
            },
            currentRow: null
        };
    },
    methods: {
        getSummaries(param) {
            const { columns, data } = param;
            const sums = [];
            columns.forEach((column, index) => {
                if (index === 0) {
                    sums[index] = '总价';
                    return;
                }
                if (!this.summaryColumn?.includes(column.property)) return;
                const values = data.map(item => Number(item[column.property]));
                if (!values.every(value => isNaN(value))) {
                    sums[index] = values.reduce((prev, curr) => {
                        const value = Number(curr);
                        if (!isNaN(value)) {
                            return (Number(prev) + curr).toFixed(2);
                        } else {
                            return prev;
                        }
                    }, 0);
                    sums[index] += '';
                } else {
                    sums[index] = '';
                }
            });
            this.$emit('summary-change', sums[sums.length - 1]);

            return sums;
        },

        // 表格当前行改变时的回调
        handleCurrentChange(val) {
            let that = this;
            this.currentRow = val;
            this.$nextTick(() => {
                if (that.$el.querySelector('tr.current-row')) {
                    let current = that.$el.querySelector('tr.current-row').querySelector('input[type=radio]');
                    that.index = current?.dataset.index;
                }
            });
            this.$emit('update:pagination', this.pagination);
            this.$emit('update:currentRow', val);
        },

        // 当前页改变
        handleCurrentChangePage(val) {
            let param = this.formSearch;
            param.start = val;
            param.pageSize = this.pagination.pageSize;
            this.$emit('update:pagination', this.pagination);
            this.$emit('searchHttp', param);
        },
        // 页码改变
        pageSizeChange(val) {
            let param = this.formSearch;
            param.pageSize = val;
            param.start = this.pagination.currentPage;
            this.$emit('searchHttp', param);
        },
        addRow({ property }) {
            if (property !== 'plus') return;
            const emptyRow = this.config
                .map(v => v.prop)
                .reduce((pre, cur) => {
                    pre[cur] = '';
                    return pre;
                }, {});
            this.data.push(emptyRow);
            this.$emit('update:data', this.data);
        },
        popRow(index) {
            this.data.splice(index, 1);
            this.$emit('popRow', this.data.length);
        }
    }
};
